﻿using MiniFox.Components;
using MiniFox.Data.EntityFramework;
using MiniFox.Platform.OAuth2;
using MiniFox.Platform.OAuth2.Providers;
using MiniFox.Web;

namespace OAuth2.Data
{
    /// <summary>
    /// 
    /// </summary>
    [AutoResolve]
    public class AuthorizationProvider : EntityDataProvider<PlatformDbContext>, IAuthorizationProvider
    {
        /// <summary>
        /// 
        /// </summary>
        public AuthorizationProvider()
        {

        }
        public bool CheckClient(string appId, string fullURI)
        {
            var app = this.CurrentDbContext.SysApp.SingleOrDefault(a => a.AppId == appId);
            if (app == null)
                return false;
            if (string.IsNullOrEmpty(app.Host))
            {
                return false;
            }
            if (!int.TryParse(app.Port, out int port))
            {
                port = app.Ssl ? 443 : 80;
            }
            string fullhost = WebUtil.BuildFullUrl(app.Ssl, app.Host, port, app.Path, string.Empty, string.Empty, string.Empty);
            string url = fullURI.ToLower();
            return url.StartsWith(fullhost.ToLower());
        }

        public bool SaveAuthorizeCode(PassportToken passportToken, string authCode, string appId, string scope, string context, int codeExpires)
        {
            SysAuthCode sys_authCode = this.CurrentDbContext.SysAuthCode.SingleOrDefault(c => c.TicketId == passportToken.TicketID && c.AppId == appId);
            bool newCode = sys_authCode == null;
            if (newCode)
            {
                sys_authCode = new SysAuthCode();
                sys_authCode.TicketId = passportToken.TicketID;
                sys_authCode.AppId = appId;
            }
            sys_authCode.Code = authCode;
            sys_authCode.CreateTime = DateTime.Now;
            sys_authCode.ExpireTime = sys_authCode.CreateTime.AddSeconds(codeExpires);
            sys_authCode.Context = context;
            sys_authCode.Scope = scope;
            int rowCount = 0;
            if (newCode)
            {
                rowCount = this.Insert(sys_authCode);
            }
            else
            {
                rowCount = this.Update(sys_authCode);
            }
            return rowCount > 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="authTicket"></param>
        /// <param name="oldPwd"></param>
        /// <param name="newPwd"></param>
        /// <returns></returns>
        public bool ResetPassword(string authTicket, string oldPwd, string newPwd)
        {
            SysPassportToken sys_token = this.CurrentDbContext.SysPassportToken.SingleOrDefault(t => t.TicketId == authTicket);

            var sys_password = this.CurrentDbContext.SysPassword.SingleOrDefault(p => p.UserId == sys_token.UserId);
            if (sys_password == null || !string.Equals(sys_password.Password, oldPwd, StringComparison.OrdinalIgnoreCase))
            {
                return false;
            }

            sys_password.Password = newPwd;
            return this.Update(sys_password) > 0;
        }
    }
}
